/*
* @Author: gaara  
* @Date: 2020-06-13 11:55:34  
*/
import _path from 'path'
import _fs from 'fs-extra'

import { RawAsset } from "./RawAsset";
import { SpriteFrame } from './SpriteFrame';
import { ImgUtil } from '../ImgUtil';

/**
 * 图片文件
 */
export class Texture extends RawAsset {

    type: string;

    wrapMode: string;

    filterMode: string;

    premultiplyAlpha: boolean;

    genMipmaps: boolean;
    packable: boolean;

    platformSettings: any;

    width: number;
    height: number;

    constructor(path: string) {
        super(path);

        this.type = "";
        this.wrapMode = "clamp";
        this.filterMode = "bilinear";
        this.premultiplyAlpha = true;
        this.genMipmaps = false;
        this.packable = false;
        this.width = 0;
        this.height = 0;
        this.platformSettings = {};
    }

    static version() {
        return '2.3.5';
    }

    static defaultType(): string[] {
        return [".png", ".jpg"];
    }

    /**
     * 是否存在meta
     */
    check() {
        let metaPath = this.getMetaPath();

        let size;
        try {
            size = ImgUtil.getImgSize(this.__path__);
        } catch (error) {
            throw '解析图片异常:' + this.__path__;
        }

        if (!size) {
            return false;
        }

        let isChangeSize: boolean = false;
        let json = _fs.readJSONSync(metaPath);

        if (json.width != size.width || json.height != size.height) {

            isChangeSize = true;
        }

        if (isChangeSize) {
            _fs.removeSync(metaPath);
            return false;
        }

        return true;
    }

    importPath() {

        let key = this.getName(false);

        let __path__ = this.__path__;
        let subp = _path.join(__path__, key);
        let sf = this.__subMetas__[key] = new SpriteFrame(subp);
        sf.importPathSub(__path__);

        sf.rawTextureUuid = this.uuid;

        this.width = sf.width;
        this.height = sf.height;

        //plist存在图片meta就保持空的
        let ext = _path.extname(__path__);

        let plistPath = __path__.replace(ext, ".plist");

        let plistExist = _fs.existsSync(plistPath);

        //fgui
        let binPath = __path__.split("_atlas")[0] + '.bin';
        let binExist = _fs.existsSync(binPath);

        //检查spine图集
        let parentFolder = _path.dirname(__path__);
        let currFiles = _fs.readdirSync(parentFolder);
        let hasSpine = false;
        for (const file of currFiles) {
            if (_path.extname(file) == ".skel") {
                hasSpine = true;
                break;
            }
        }

        //spine不预乘
        this.premultiplyAlpha = !hasSpine;

        let isRaw = binExist || plistExist || hasSpine;
        this.packable = !isRaw;
        this.type = isRaw ? "raw" : "sprite";
        if (isRaw) {
            this.__subMetas__ = {};
        }

    }
}